<?php
namespace App\Http\Middleware;

use App\Wen\Utils\Settings;
use Illuminate\Http\Request;
use Closure;

class WebModeProcess{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $pc_mode = Settings::get('pc_mdoe', 0);
        if($pc_mode != 0){
            $requestUri = $request->getRequestUri();
            $range = Settings::get('pc_mdoe_page_range', []);

            if($range && $requestUri){
                $_301_url = Settings::get('pc_mdoe_301_url', '')  . '?from=' . urlencode(''.$requestUri);
                if($requestUri == '/'){
                    if(in_array('1', $range)){
                        // 首页
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            $html_code = Settings::get('pc_mdoe_html_code', '默认页面，请去pc端配置-开关-html页面代码');
                            return response($html_code);
                        }
                    }else{
                        return $next($request);
                    }
                }
                $requestUri_arr = explode('/', $requestUri);
                $url_prefix = $requestUri_arr[1];

                if($url_prefix == 'hot'){
                    if(in_array('2', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'tag'){
                    if(in_array('3', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'design'){
                    if(in_array('4', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'contact'){
                    if(in_array('5', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'circles'){
                    if(in_array('6', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'circle'){
                    if(in_array('7', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'clause'){
                    if(in_array('8', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'login'){
                    if(in_array('9', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'search'){
                    if(in_array('10', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'single'){
                    if(in_array('11', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == 'tool'){
                    if(in_array('12', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else if($url_prefix == '404'){
                    if(in_array('13', $range)){
                        if($pc_mode == 1){
                            return response()->redirectTo($_301_url, 301);
                        }else if($pc_mode == 2){
                            return response()->redirectTo('/', 301);
                        }
                    }
                }else{

                    if($pc_mode == 1){
                        return response()->redirectTo($_301_url, 301);
                    }else if($pc_mode == 2){
                        return response()->redirectTo('/', 301);
                    }
                }
            }
        }

        return $next($request);
    }
}
